// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import yyComponent from "../Common/yyComponent";
import { sers } from "../frameSccript/system/OverallSystem ";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CityNode extends yyComponent {

    // LIFE-CYCLE CALLBACKS:


    @property(cc.Node)
    cityNode: cc.Node = null

    @property(cc.ScrollView)
    scr: cc.ScrollView = null

    @property(cc.Node)
    content: cc.Node = null

    // onLoad () {}
    @property(cc.Label)
    myCityTxt: cc.Label = null


    dingbujuli = 400;
    jianju = 550
    xianshijuli = 100
    _nodeList = [];
    curRSVNum = -1;
    cityRankData = []

    start() {

    }



    scrollviewEvent() {
        var that = this;
        var scrollView = this.scr;
        var content = this.content;
        var nodeTly = scrollView.getScrollOffset().y;

        let cityLeng = 10
        var len = Math.floor((nodeTly - this.xianshijuli) / this.jianju);
        if (scrollView.getScrollOffset().y > this.dingbujuli, len >= 0) {
            if (this._nodeList.length < 1 && content.children.forEach(function (nestedVariable) {
                if ("cityRank" != nestedVariable.name) {
                    that._nodeList.push(nestedVariable);
                }
            }), len > this.curRSVNum) {
                var node = this._nodeList.splice(0, 1)[0];
                this._nodeList.push(node);
                this.curRSVNum = len;
                var i = len;
                for (; i < len + 3; i++) {
                    var model = this._nodeList[i - len];
                    if (i < cityLeng && model) {
                        model.y = -this.dingbujuli - this.jianju * i;
                        if (model.getChildByName("rank").getChildByName("cityName").getComponent(cc.Label).string != this.cityRankData[i].cityName) {
                            this.showCityInfo(model, this.cityRankData[i]);
                        }
                    }
                }
            } else {
                if (len < this.curRSVNum) {
                    this._nodeList.reverse();
                    node = this._nodeList.splice(0, 1)[0];
                    this._nodeList.push(node);
                    this._nodeList.reverse();
                    this.curRSVNum = len;
                    i = len;
                    for (; i < len + 3; i++) {
                        var model = this._nodeList[i - len];
                        if (i < cityLeng && model) {
                            model.y = -this.dingbujuli - this.jianju * i;
                            if (model.getChildByName("rank").getChildByName("cityName").getComponent(cc.Label).string != this.cityRankData[i].cityName) {
                                this.showCityInfo(model, this.cityRankData[i]);
                            }
                        }
                    }
                }
            }
        }
    }

    updataCity() {
        let that = this;
        let UserData = sers().userSrv.getUserData()
        if ("" == UserData.city) {
            that.myCityTxt.string = "您还没有加入地区";
        } else {
            that.cityRankData.forEach(function (cityInfo, x) {
                if (cityInfo.cityName == UserData.default.getInstance().city) {
                    // GameConstant.GameConstant.cityRank = cityInfo;
                    if (UserData.default.getInstance().daywinnum > 0) {
                        that.myCityTxt.string = "我的地区排名:第" + cityInfo.rank + "名";
                    } else {
                        that.myCityTxt.string = "通关助力地区(当前第" + cityInfo.rank + "名)";
                    }
                }
            });
        }
    }

    showCityInfo(model, v) {
        var concreteChildren = model.getChildByName("niao").children;
        var scene = model.getChildByName("rank");
        var streamToStop = model.getChildByName("isMe");
        streamToStop.active = false;
        var x = Math.random() > .5 ? -1 : 1;
        concreteChildren.forEach(function (t, j) {
            if (j < v.score) {
                t.active = true;
                // Tools.default.playSpine(t, "1");
                t.scaleX = x;
            } else {
                t.active = false;
            }
        });
        // if (v.cityName == UserData.default.getInstance().city && UserData.default.getInstance().daywinnum > 0) {
        //     streamToStop.active = true;
        //     this.isMeSpine = concreteChildren[0];
        //     this.showMeSpine();
        // }
        if (1 != v.rank) {
            scene.getChildByName("no1").active = false;
            scene.getChildByName("rankTxt").color = (new cc.Color).fromHEX("#000000");
            if (model.getChildByName('tai1')) {
                model.getChildByName('tai1').active = true
            }
        } else {
            scene.getChildByName("no1").active = true;
            scene.getChildByName("rankTxt").color = (new cc.Color).fromHEX("#FFFFFF");
            if (model.getChildByName('tai1')) {
                model.getChildByName('tai1').active = false
            }
        }
        scene.getChildByName("rankTxt").getComponent(cc.Label).string = "第" + v.rank + "名";
        scene.getChildByName("niaoNum").getComponent(cc.Label).string = v.score + "只猫";
        var text = v.cityName;
        if (!("省" != text.slice(text.length - 1, text.length) && "市" != text.slice(text.length - 1, text.length))) {
            text = text.slice(0, text.length - 1);
        }
        scene.getChildByName("cityName").getComponent(cc.Label).string = text + "舞台";
    }

    // update (dt) {}
}
